var app = getApp();
Page({
  data: {
    email: "",
    pwd: "",
    phone_code:"",
    btntext:" 点击获取验证码",
    disabled:"",
    fid:"",
    cartItems:[],
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function (options) {
    var cartItems = wx.getStorageSync('cartItems') || []
      for (var i = 0; i < cartItems.length; i++) {
        delete cartItems[i].img_url;
        delete cartItems[i].name; 
        delete cartItems[i].old_price; 
        delete cartItems[i].price;  
    }

    this.setData({
      cartItems: cartItems
    })
    
    let token = wx.getStorageSync('token');
    if (token){
      wx.switchTab({
        url:'../index/index'
      })
    }

    let id = 0;
    let that = this;
    if (options.scene) {
      const scene = decodeURIComponent(options.scene)
      id = scene.substring(3);
      console.log(id)
      that.setData({
        fid: id
      });
    }
  },

  getPhoneValue(e) {
    this.setData({
      email: e.detail.value
    })
  },

  getPasswordValue(e) {
    this.setData({
      pwd: e.detail.value
    })
  },

  bindvalue(e) { // 实时监听表单输入的值
    this.setData({
      phone_code: e.detail.value
    })
  },

  verification() { 
    var that = this;

    var email = this.data.email;
     wx.request({
       url: app.globalData.url +'index.php?_m=index/wx/phone_code',
       header: {
         'content-type': 'application/x-www-form-urlencoded'
       },
       data: { phone: email },
       method: "POST",
       success(res) {
        if (res.data.code == 0) {
          var coden = 60    // 定义60秒的倒计时
          var codeV = setInterval(function () {
            that.setData({    // _this这里的作用域不同了
              btntext: (--coden) + 's' + '后重新获取',
              disabled:'disabled'
            })
            if (coden == -1) {  // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
              clearInterval(codeV)
              that.setData({
                btntext: '获取验证码',
                disabled: ''
              })
            }
          }, 1000)  //  1000是1秒
        }else{
          wx.showModal({
            title: "提示",
            content: res.data.msg
          })
        }

      },
     }) 
  },

  loginSubmit: function (e) {
    let that = this;
    let email = that.data.email;
    let phone_code = that.data.phone_code;
    let pwd = that.data.pwd;
    let fid = that.data.fid;
    let cart_json = JSON.stringify(that.data.cartItems);

    wx.showLoading({
      title: '加载中...',
    })
    wx.login({
      success(res) {
        if (res.code) {
            wx.getUserInfo({
              success: function (resInfo) {
                  wx.request({
                    url: app.globalData.url +'index.php?_m=index/wx/token',
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      code: res.code,
                      rawData: resInfo.rawData,
                      signature: resInfo.signature,
                      iv: resInfo.iv,
                      encryptedData: resInfo.encryptedData,
                      email: email,
                      type: 'reg',
                      pwd: pwd,
                      fid: fid,
                      phone_code: phone_code ,
                      cart_json: cart_json
                    },
                    method: "POST",
                    success(res) {
                      wx.hideLoading();
                      if (res.data.code == 0) {
                        wx.removeStorageSync("cartItems");
                        app.globalData.token = res.data.token;
                        wx.setStorageSync('token', app.globalData.token);
                        app.globalData.uid = res.data.uid;
                        wx.setStorageSync('uid', app.globalData.uid);
                        app.globalData.email = email;
                        wx.setStorageSync('email', app.globalData.email);
                        app.globalData.pwd = pwd;
                        wx.setStorageSync('pwd', app.globalData.pwd);
                        wx.showToast({
                          title: '注册成功',
                          icon: 'none',
                          duration: 3000
                        })
                        setTimeout(function () {
                          wx.reLaunch({
                            url: '/pages/index/index'
                          })
                        }, 3000)
                        
                      } else {
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none',
                          duration: 3000
                        })
                        setTimeout(function () {
                          wx.hideToast()
                        }, 3000)
                        return false;
                      }
                    }


                  })
              }
            })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 3000
          })
        }    
      }
    })

  },

  bindGetUserInfo: function (e) {

    let that = this;
    let email = app.globalData.email;
    let pwd = app.globalData.pwd;
    let cart_json = JSON.stringify(that.data.cartItems);
    
    wx.showLoading({
      title: '加载中...',
    })
    wx.login({
      success(res) {
        if (res.code) {
          wx.getUserInfo({
            success: function (resInfo) {     
                wx.request({
                  url: app.globalData.url +'index.php?_m=index/wx/token',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  data: {
                    code: res.code,
                    rawData: resInfo.rawData,
                    signature: resInfo.signature,
                    iv: resInfo.iv,
                    encryptedData: resInfo.encryptedData,
                    type: 'weixin',
                    email: email,
                    pwd: pwd,
                    cart_json: cart_json
                  },
                  method: "POST",
                  success(res) {
                    wx.hideLoading();

                    if (res.data.code == 0) {
                      wx.removeStorageSync("cartItems");
                      app.globalData.token = res.data.token;
                      wx.setStorageSync('token', app.globalData.token);
                      app.globalData.uid = res.data.uid;
                      wx.setStorageSync('uid', app.globalData.uid);
                      wx.reLaunch({
                        url: '/pages/index/index'
                      })
                    
                    } else {
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 3000
                      })
                      setTimeout(function () {
                        wx.hideToast()
                      }, 3000)
                      return false;
                    }
                  }


                })
            }
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 3000
          })
        }   
      }
    })

  },

  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }




})